package com.nowcoder.community.controller;

import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.pojo.Event;
import com.nowcoder.community.pojo.User;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.RedisKeyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Api(description = "点赞相关")
@Controller
public class LikeController implements CommunityConstant{

    @Autowired
    @Qualifier("likeService")
    private LikeService likeService;

    @Autowired
    private EventProducer producer;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "点赞接口")
    @PostMapping("/like")
    @ResponseBody
    @ApiImplicitParams({
        @ApiImplicitParam(name = "entityType", value = "分页实例"),
        @ApiImplicitParam(name = "entityId", value = "帖子显示的方式"),
        @ApiImplicitParam(name = "entityUserId", value = "实体对应的用户"),
        @ApiImplicitParam(name = "discussPostId", value = "帖子id")
    })
    public String like(int entityType, int entityId, int entityUserId, int discussPostId){

        User user = hostHolder.getUser();

        //点赞
        likeService.like(user.getId(), entityType, entityId, entityUserId);

        //统计当前实体被点赞的数量
        long likeCount = likeService.findEntityLikeCount(entityType, entityId);

        //统计当前实体被赞的状态（已赞或未赞）
        int likeStatus = likeService.findEntityLikeStatus(user.getId(), entityType, entityId);

        //返回的结果（存到map中）
        Map<String, Object> map = new HashMap<>();
        map.put("likeCount", likeCount);
        map.put("likeStatus", likeStatus);

        //上面的操作完成后，触发点赞事件
        if (likeStatus == 1){//已赞状态
            Event event = new Event()
                    .setTopic(TOPIC_LIKE)
                    .setUserId(hostHolder.getUser().getId())
                    .setEntityType(entityType)
                    .setEntityId(entityId)
                    .setEntityUserId(entityUserId)
                    .setData("discussPostId", discussPostId);//点赞了在哪个帖子
            producer.fireEvent(event);
        }


        //当实体类型为帖子时，计算帖子分数
        if(entityType == ENTITY_TYPE_POST){
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey, discussPostId);
        }

        return CommunityUtil.getJSONString(0, null, map);
    }
}
